package com.studyweb.service.impl;

import com.studyweb.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import jakarta.mail.internet.MimeMessage;

/**
 * 邮件服务实现类
 * 
 * @author StudyWeb Team
 * @since 1.0.0
 */
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    /**
     * 发送验证码邮件
     * 
     * @param email 收件人邮箱地址
     * @param verificationCode 验证码
     * @return boolean 发送是否成功
     */
    @Override
    public boolean sendVerificationCode(String email, String verificationCode) {
        try {
            String subject = "StudyWeb - 密码重置验证码";
            String htmlContent = buildVerificationCodeHtml(verificationCode);
            
            return sendHtmlEmail(email, subject, htmlContent);
        } catch (Exception e) {
            log.error("发送验证码邮件失败，邮箱：{}，错误：{}", email, e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发送简单文本邮件
     * 
     * @param to 收件人邮箱地址
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return boolean 发送是否成功
     */
    @Override
    public boolean sendSimpleEmail(String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(fromEmail);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);
            
            mailSender.send(message);
            log.info("简单邮件发送成功，收件人：{}", to);
            return true;
        } catch (Exception e) {
            log.error("发送简单邮件失败，收件人：{}，错误：{}", to, e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发送HTML格式邮件
     * 
     * @param to 收件人邮箱地址
     * @param subject 邮件主题
     * @param htmlContent HTML格式的邮件内容
     * @return boolean 发送是否成功
     */
    @Override
    public boolean sendHtmlEmail(String to, String subject, String htmlContent) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            
            helper.setFrom(fromEmail);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlContent, true);
            
            mailSender.send(message);
            log.info("HTML邮件发送成功，收件人：{}", to);
            return true;
        } catch (Exception e) {
            log.error("发送HTML邮件失败，收件人：{}，错误：{}", to, e.getMessage(), e);
            return false;
        }
    }

    /**
     * 构建验证码邮件的HTML内容
     * 
     * @param verificationCode 验证码
     * @return String HTML内容
     */
    private String buildVerificationCodeHtml(String verificationCode) {
        return "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset='UTF-8'>\n" +
                "    <title>密码重置验证码</title>\n" +
                "    <style>\n" +
                "        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }\n" +
                "        .container { max-width: 600px; margin: 0 auto; padding: 20px; }\n" +
                "        .header { background-color: #007bff; color: white; padding: 20px; text-align: center; }\n" +
                "        .content { padding: 30px; background-color: #f8f9fa; }\n" +
                "        .code { font-size: 24px; font-weight: bold; color: #007bff; text-align: center; \n" +
                "                padding: 15px; background-color: #e9ecef; border-radius: 5px; margin: 20px 0; }\n" +
                "        .footer { padding: 20px; text-align: center; color: #666; font-size: 12px; }\n" +
                "        .warning { color: #dc3545; font-weight: bold; }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class='container'>\n" +
                "        <div class='header'>\n" +
                "            <h1>StudyWeb 密码重置</h1>\n" +
                "        </div>\n" +
                "        <div class='content'>\n" +
                "            <h2>您好！</h2>\n" +
                "            <p>您正在申请重置StudyWeb账户密码，请使用以下验证码完成密码重置：</p>\n" +
                "            <div class='code'>" + verificationCode + "</div>\n" +
                "            <p><span class='warning'>重要提醒：</span></p>\n" +
                "            <ul>\n" +
                "                <li>验证码有效期为5分钟，请尽快使用</li>\n" +
                "                <li>如果这不是您的操作，请忽略此邮件</li>\n" +
                "                <li>为了您的账户安全，请勿将验证码告诉他人</li>\n" +
                "            </ul>\n" +
                "            <p>如有疑问，请联系我们的客服团队。</p>\n" +
                "        </div>\n" +
                "        <div class='footer'>\n" +
                "            <p>此邮件由系统自动发送，请勿回复</p>\n" +
                "            <p>&copy; 2024 StudyWeb. All rights reserved.</p>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
    }
}